/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package l1j.server.server.model.item.executor;

import l1j.server.server.packets.server.S_Dexup;
import l1j.server.server.packets.server.S_SkillSound;
import l1j.server.server.packets.server.S_ServerMessage;
import l1j.server.server.packets.server.S_Strup;
import static l1j.server.server.model.skill.L1SkillId.*;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import l1j.server.server.datatables.ItemTable;
import l1j.server.server.model.skill.L1BuffUtil;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.utils.PerformanceTimer;

@XmlAccessorType(XmlAccessType.FIELD)
public class L1FloraPotion {

	private static Logger _log = Logger.getLogger(L1FloraPotion.class.getName());
	
	@XmlAccessorType(XmlAccessType.FIELD)
	@XmlRootElement(name = "ItemEffectList")
	private static class ItemEffectList implements Iterable<L1FloraPotion> {
		@XmlElement(name = "Item")
		private List<L1FloraPotion> _list;

		public Iterator<L1FloraPotion> iterator() {
			return _list.iterator();
		}
	}

	@XmlAccessorType(XmlAccessType.FIELD)
	private static class Effect {
		@XmlAttribute(name = "Time")
		private int _time;
		
		private int getTime() {
			return _time;
		}
		
		@XmlAttribute(name = "Str")
		private int _str = 0;
		
		private int getStr() {
			return _str;
		}
		
		@XmlAttribute(name = "Dex")
		private int _dex = 0;
		
		private int getDex() {
			return _dex;
		}
	}

	private static final String PATH = "./data/xml/Item/FloraPotion.xml";

	private static final HashMap<Integer, L1FloraPotion> _dataMap = new HashMap<Integer, L1FloraPotion>();

	public static L1FloraPotion get(int id) {
		return _dataMap.get(id);
	}

	@XmlAttribute(name = "ItemId")
	private int _itemId;

	private int getItemId() {
		return _itemId;
	}

	@XmlElement(name = "Effect")
	private Effect _effect;
	
	private Effect getEffect() {
		return _effect;
	}

	public static void load() {
		PerformanceTimer timer = new PerformanceTimer();
		System.out.print("loading flora potion...");
		try {
			JAXBContext context = JAXBContext.newInstance(L1FloraPotion.ItemEffectList.class);

			Unmarshaller um = context.createUnmarshaller();

			File file = new File(PATH);
			ItemEffectList list = (ItemEffectList) um.unmarshal(file);

			for (L1FloraPotion each : list) {
				if (ItemTable.getInstance().getTemplate(each.getItemId()) == null) {
					System.out.println("アイテムID " + each.getItemId() + " のテンプレートが見つかりません。");
				} else {
					_dataMap.put(each.getItemId(), each);
				}
			}
		} catch (Exception e) {
			_log.log(Level.SEVERE, PATH + "のロードに失敗。", e);
			System.exit(0);
		}
		System.out.println("OK! " + timer.elapsedTimeMillis() + "ms");
	}

	public boolean use(L1PcInstance pc) {
		if (pc.hasSkillEffect(71) == true) { // ディケイ ポーションの状態
			pc.sendPackets(new S_ServerMessage(698)); // 魔力によって何も飲むことができません。
			return false;
		}

		// アブソルート バリアの解除
		L1BuffUtil.cancelBarrier(pc);
		
		Effect effect = getEffect();
		if (effect.getStr() > 0) { // STR上昇効果 (激励,フローラ)
			if (pc.hasSkillEffect(PHYSICAL_ENCHANT_STR))
				pc.removeSkillEffect(PHYSICAL_ENCHANT_STR);
			if (pc.hasSkillEffect(DRESS_MIGHTY))
				pc.removeSkillEffect(DRESS_MIGHTY);
			if (!pc.hasSkillEffect(STATUS_FLORA_POTION_STR))
				pc.addStr(effect.getStr());
			pc.sendPackets(new S_Strup(pc, effect.getStr(), effect.getTime()));
			pc.setSkillEffect(STATUS_FLORA_POTION_STR, effect.getTime() * 1000);
		}
		if (effect.getDex() > 0) { // DEX上昇効果 (才能,フローラ)
			if (pc.hasSkillEffect(PHYSICAL_ENCHANT_DEX))
				pc.removeSkillEffect(PHYSICAL_ENCHANT_DEX);
			if (pc.hasSkillEffect(DRESS_DEXTERITY))
				pc.removeSkillEffect(DRESS_DEXTERITY);
			if (!pc.hasSkillEffect(STATUS_FLORA_POTION_DEX))
				pc.addDex(effect.getDex());
			pc.sendPackets(new S_Dexup(pc, effect.getDex(), effect.getTime()));
			pc.setSkillEffect(STATUS_FLORA_POTION_DEX, effect.getTime() * 1000);
		}

		pc.sendPackets(new S_SkillSound(pc.getId(), 191));
		pc.broadcastPacket(new S_SkillSound(pc.getId(), 191));
		
		return true;
	}

}
